package cn.anlab.anappframe.demo.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import cn.anlab.anappframe.R;
import cn.anlab.anappframe.demo.DemoDataUtil;
import cn.anlab.anappframe.demo.adapter.ListViewAdapter;
import cn.anlab.anappframe.widget.CompListView;

public class ListViewDemoActivity extends Activity {

	CompListView lv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.demo_listview);
		
		LinearLayout container = (LinearLayout)findViewById(R.id.container);
		
//		ListView lv = (ListView)findViewById(R.id.listView1);
//		//启用快速滚动块
//		lv.setFastScrollEnabled(true);
//		
//		ListViewAdapter adapter = new ListViewAdapter(this, DemoDataUtil.getHomeItem(45));
//		lv.setAdapter(adapter);
		
		
		
		lv = new CompListView(this, CompListView.PROP_FOOTER_LOADING |CompListView.PROP_HEADER_LOADING);
//		lv.setHeaderVisibility(View.VISIBLE);
		lv.setFooterState(CompListView.FOOTER_STATE_MORE_BUTTON);
		
		//启用快速滚动块
		lv.setFastScrollEnabled(true);
		
		ListViewAdapter adapter = new ListViewAdapter(this, DemoDataUtil.getHomeItem(45), "name");
		lv.setAdapter(adapter);
		
		LinearLayout.LayoutParams prm = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
		container.addView(lv);
		
		//行点击事件
		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Log.i("anlog", "position : " + position);
			}
		});
		
		//滚动加载
		lv.setOnScrollListener(lvOnScrollListener);
		
		
		
		
		
	}
	
	//滚动事件
	private OnScrollListener lvOnScrollListener = new OnScrollListener() {
		
		private int firstVisibleItem;
		
		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			Log.i("anlog", "scrollState : " + scrollState);
			if(firstVisibleItem == 0) {
				if(scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
					lv.setHeaderState(CompListView.HEADER_STATE_LOADING);
				} else {
					lv.setHeaderState(CompListView.HEADER_STATE_TO_RELEASE);
				}
			}
		}
		
		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
			Log.i("anlog", "firstVisibleItem : " + firstVisibleItem + ", visibleItemCount : " + visibleItemCount + ", totalItemCount : " + totalItemCount);
			this.firstVisibleItem = firstVisibleItem;
		}
	};
	

}


//private int lastItemIndex = 0;
//
//@Override
//public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
////	Log.i("CompListView", "first : " + firstVisibleItem + ", visible : " + visibleItemCount + ", total : " + totalItemCount);
//	lastItemIndex = firstVisibleItem + visibleItemCount;
//}
//@Override
//public void onScrollStateChanged(AbsListView view, int scrollState) {
//	if ((scrollState == OnScrollListener.SCROLL_STATE_IDLE) && lastItemIndex == adapter.getCount() + 1) {
//		if(hasMore) {
//			loadDataFromBO();
//		} else {
//			Toast.makeText(context, MESSAGE_NO_DATA, Toast.LENGTH_SHORT).show();
//		}
//	}
//}
